<?php
include('./sdk/TieTuKu.class.php');
$config = require('./config.php');
$sdk = new TTKClient($config['ak'], $config['sk']);
$x = $_POST['x'];
$y = $_POST['y'];
$w = $_POST['w'];
$h = $_POST['h'];
$url = $_POST['url'];
$jpeg_quality = 85;
$imginfo = getimagesize($url);
$type_list = array("1"=>"gif","2"=>"jpg","3"=>"png","4"=>"swf","5" => "psd","6"=>"bmp","15"=>"wbmp");
$ext = '';
if($type_list[$imginfo['2']]){
	$ext = $type_list[$imginfo['2']];
} else {
	exit('err');
}
$targ_w = $imginfo[0];
$targ_h = $imginfo[1];
switch ($ext) {
    case 'jpg':
        $img_r = imagecreatefromjpeg($url);
        break;
    case 'png':
        $img_r = imagecreatefrompng($url);
        break;
    case 'gif':
        $img_r = imagecreatefromgif($url);
        break;
    default:
        $img_r = imagecreatefromjpeg($url);
        break;
}
$dst_r = ImageCreateTrueColor($targ_w, $targ_h);
imagecopyresampled($dst_r,$img_r,0,0,$x,$y,$targ_w,$targ_h,$w,$h);
$str =  cname();
$dirname = './delfile/'.$str.'.jpg';
imagejpeg($dst_r,$dirname,$jpeg_quality);
$return = $sdk->uploadFile($config['ab'], $dirname);
if(!$return){
	return $return;
}
function cname() {
	$str = time();
	for($i = 0; $i < 9; $i++) {
	    $str .= mt_rand(0, 9);
	}
	return $str;
}